package com.atguigu.jxc.controller;

import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.entity.GoodsType;
import com.atguigu.jxc.entity.vo.GoodsTypeVo;
import com.atguigu.jxc.service.GoodsTypeService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 *
 *
 * @author Mr.zhao
 * @version 1.0
 * @date 2021/12/31 10:07
 */
@RestController
@RequestMapping("/goodsType")
public class GoodTypeController {

    @Autowired
    private GoodsTypeService goodsTypeService;

    /**
     * http://localhost:8080/goodsType/loadGoodsType
     * @return
     */
    @PostMapping("/loadGoodsType")
    public String getGoodsType(){
        List<GoodsTypeVo> list = goodsTypeService.getGoodsTypes();


        ObjectMapper objectMapper = new ObjectMapper();
        String string = null;
        try {
            string = objectMapper.writeValueAsString(list);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        System.out.println(string);
        return string;
    }

    @PostMapping("/save")
    public ServiceVO saveGoodsType(String  goodsTypeName,Integer  pId){
        goodsTypeService.save(goodsTypeName,pId);

        return ServiceVO.Ok();
    }

    /**
     * http://localhost:8080/goodsType/delete
     */
    @PostMapping()
    public ServiceVO deleteGoodsType(Integer  goodsTypeId){
        goodsTypeService.delete(goodsTypeId);
        return ServiceVO.Ok();
    }


}
